﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAn.BUS;
using DoAn.DTO;
using System.Data;

namespace DoAn
{
    public partial class TimKiem : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                //load dropdownlist
                loaddropdownlist();

            }

        }
        void loaddropdownlist()
        {
            
            DataTable dt = DanhMucBUS.loadDanhMucCha();
            ListItem t = new ListItem();
            t.Text = "-----Chon------";
            t.Value = "-1";
            DropDownList1.Items.Add(t);
            foreach(DataRow i in dt.Rows)
            {
                ListItem temp = new ListItem();
                temp.Text = i["TenDanhMuc"].ToString();
                temp.Value = i["MaDanhMuc"].ToString();
                DropDownList1.Items.Add(temp);
            }
        }
        void loadsd(int ma)
        {
            List<int> t = DanhMucBUS.laylistma(ma);
            DataList1.DataSource = SanDauGiaBUS.laydssantheomaDM(t);
            DataList1.DataBind();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            string sql = "select * from SANDAUGIA Where ";
            int dem = 0;
            if (txttensp.Text != "")
            {
                
                sql += "TenSan Like N'" + "%" + txttensp.Text + "%" + "'";
                dem++;
            }
           
            if (float.Parse( txtgiakhoidiem_tu.Text) != 0)
            { 
                if(dem >0)
                    sql += "and GiaKhoiDiem>='"+txtgiakhoidiem_tu.Text+"'";
                else
                    sql += " GiaKhoiDiem>='" + txtgiakhoidiem_tu.Text + "'";
                dem++;
            }
            if (float.Parse(txtgiakhoidiem_den.Text) != 0)
            {
                if (dem > 0)
                    sql += "and GiaKhoiDiem<='" + txtgiakhoidiem_den.Text + "'";
                else
                    sql += " GiaKhoiDiem<='" + txtgiakhoidiem_den.Text + "'";
                dem++;
            }

            if (float.Parse(txtgiahienthoi_tu.Text) != 0)
            {
                if (dem > 0)
                    sql += "and GiaSanHienTai>='" + txtgiahienthoi_tu.Text + "'";
                else
                    sql += " GiaSanHienTai>='" + txtgiahienthoi_tu.Text + "'";
                dem++;
            }
            if (float.Parse(txtgiahienthoi_den.Text) != 0)
            {
                if (dem > 0)
                    sql += "and GiaSanHienTai<='" + txtgiahienthoi_den.Text + "'";
                else
                    sql += " GiaSanHienTai<='" + txtgiahienthoi_den.Text + "'";
                dem++;
            }

            if (txtngayketthuc_tu.Text != "")
            {
                if (dem > 0)
                    sql += "and NgayKetThucSan>='" + txtngayketthuc_tu.Text + "'";
                else
                    sql += " NgayKetThucSan>='" + txtngayketthuc_tu.Text + "'";
                dem++;
            }
            if (txtngayketthuc_den.Text != "")
            {
                if (dem > 0)
                    sql += "and NgayKetThucSan<='" + txtgiahienthoi_den.Text + "'";
                else
                    sql += " NgayKetThucSan<='" + txtgiahienthoi_den.Text + "'";
                dem++;
            }
            //tim
            

            if (Convert.ToInt32( DropDownList1.SelectedValue.ToString()) == -1)
            {
                if(dem >0)
                {
                    DataTable dt = SanDauGiaBUS.timkiemkhung(sql);
                    DataList1.DataSource = dt;
                    DataList1.DataBind();
                }
                else
                {
                    Response.Write("<script>alert('Chua Nhap Thong tin ')</script>");
                    return;
                }

                
            }
            else
            {
                int ma = Convert.ToInt32(DropDownList1.SelectedValue.ToString());
                List<int> t = DanhMucBUS.laylistma(ma);
                if (dem > 0)
                {
                    DataTable dt = SanDauGiaBUS.timkiemkhung(sql);
                    
                    int kt = dt.Rows.Count;
                    for (int j = kt - 1; j >= 0; j--)
                    {
                        int k = Int32.Parse(dt.Rows[j]["MaDanhMuc"].ToString());
                        Boolean flag = false;
                        for (int i = 0; i < t.Count; i++)
                        {
                            if (k == t[i])
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (flag == false)
                            dt.Rows.RemoveAt(j);

                    }
                    DataList1.DataSource = dt;
                    DataList1.DataBind();

                }
                else
                {
                    DataList1.DataSource = SanDauGiaBUS.laydssantheomaDM(t);
                    DataList1.DataBind();
                }
            }
        }
    }
}